#!/usr/bin/python
# -*- coding: UTF-8 -*-

from urllib import request
from bs4 import BeautifulSoup
import os


class Pull:
    __url__ = ''

    def __init__(self, url=''):
        self.__url__ = url

    def set_url(self, url):
        self.__url__ = url

    def get_html(self):
        response = request.urlopen(self.__url__)
        html = response.read().decode('gbk')
        return html

    def get_content(self, name, attrs=None):
        if attrs is None:
            attrs = {}
        dom = BeautifulSoup(self.get_html(), "html.parser")
        content_html = dom.find_all(name, attrs)
        if len(content_html):
            return content_html[0]  # 不间断空白符 &nbsp;
        return '没有内容'

    def save(self, file_path, content):
        fp = open(file_path, mode='w')
        fp.write(content.text.replace('\xa0' * 8, '\n\n'))
        fp.close()


class LocalList:
    __items__ = []

    def set_items(self, items):
        self.__items__ = items

    def load(self, name, attrs=None):
        if attrs is None:
            attrs = {}
        pull = Pull()
        for i in self.__items__:
            pull.set_url(i)
            content = pull.get_content(name, attrs)
            # todo::完成写或者其他操作


